﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class loaiSiteSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public loaiSiteSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public string addLoaiSite(LOAI_SITE entity)
        {
            dbContext.LOAI_SITE.Add(entity);
            dbContext.SaveChanges();
            return "";
        }

        public string upLoaiSite(LOAI_SITE entity, string maOld)
        {
            dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return "";
        }

        public LOAI_SITE getSiteByma(string maLoaiSite)
        {

            return dbContext.LOAI_SITE.Where(p => p.MA == maLoaiSite).First();
        }

        public bool deteleLoaiSite(string maLoaiSite)
        {
            try
            {
                var entitys = getSiteByma(maLoaiSite);
                dbContext.LOAI_SITE.Remove(entitys);
                dbContext.SaveChanges();
                return true;
            }
            catch(Exception e) { return false; }
        }

        public List<LOAI_SITE> getList()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.LOAI_SITE.ToList();
            }
            catch (Exception e)
            {
                List<LOAI_SITE> _listRe = new List<LOAI_SITE>();
                return _listRe;
            }
        }

        public decimal addorUpdate_MVTLOAISITE(LOAISITE_MANGVT entity)
        {
            if (entity.ID == 0) dbContext.LOAISITE_MANGVT.Add(entity);
            else
            {
                dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            }

            dbContext.SaveChanges();
            return entity.ID;
        }

        public List<LOAISITE_MANGVT> getListLSMVT(string ma)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.LOAISITE_MANGVT.Where(p => p.LOAISITE == ma).ToList();
            }
            catch (Exception e)
            {
                List<LOAISITE_MANGVT> _listRe = new List<LOAISITE_MANGVT>();
                return _listRe;
            }
        }

        public LOAISITE_MANGVT getSiteMVTByma(decimal maLoaiSiteMVT)
        {

            return dbContext.LOAISITE_MANGVT.Where(p => p.ID == maLoaiSiteMVT).First();
        }

        public bool deteleLoaiSiteMVT(decimal maLoaiSiteMVT)
        {
            try
            {
                var entitys = getSiteMVTByma(maLoaiSiteMVT);
                dbContext.LOAISITE_MANGVT.Remove(entitys);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e) { return false; }
        }

        public List<LOP_BANDO> getList_lopbando()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.LOP_BANDO.ToList();
            }
            catch (Exception e)
            {
                List<LOP_BANDO> _listRe = new List<LOP_BANDO>();
                return _listRe;
            }
        }

        public int Create_UserSite(USER_SITE entity)
        {
            try
            {
                dbContext.USER_SITE.Add(entity);
                dbContext.SaveChanges();
                return 1;
            }
            catch { return 0; }
        }

        public int Update_UserSite(USER_SITE entity)
        {
            try
            {
                dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return 1;
            }
            catch { return 0; }
        }

    }
}
